package com.example.practice.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;

import org.w3c.dom.Text;

public class changeUserInfoActivity_yl extends AppCompatActivity {
    private TextView tv_title;
    private TextView tv_back;
    private RelativeLayout rl_title_bar;
    private TextView tv_save;
    private String title_yl, content_yl;
    private int flag_yl;
    private EditText et_content;
    private ImageView iv_delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_user_info_yl);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initView();
        getData();
        contentListener();
    }

    /**
     * 获取个人资料传递过来的数据
     */
    private void getData() {
        title_yl=getIntent().getStringExtra("title");
        content_yl=getIntent().getStringExtra("content");
        flag_yl=getIntent().getIntExtra("flag",0);
        tv_title.setText(title_yl);
        if (!TextUtils.isEmpty(content_yl)){
            et_content.setText(content_yl);
            et_content.setSelection(content_yl.length());
        }
    }

    private void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_back=findViewById(R.id.tv_back);
        rl_title_bar=findViewById(R.id.rl_bar);
        tv_save=findViewById(R.id.tv_save_yl);
        et_content=findViewById(R.id.et_content_yl);
        iv_delete=findViewById(R.id.iv_delete_yl);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeUserInfoActivity_yl.this.finish();
            }
        });

        //删除文本框中所有内容
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_content.setText("");
            }
        });

        //保存用户修改信息
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });

        tv_save.setVisibility(View.VISIBLE);

        rl_title_bar.setBackgroundColor(Color.parseColor("#30b4ff"));
    }

    private void save() {
        Intent data= new Intent();
        String etContent=et_content.getText().toString().trim();
        boolean isSave=false;
        switch (flag_yl){
            case 1:
                if(!TextUtils.isEmpty(etContent)){
                    Log.i(etContent,etContent);
                    data.putExtra("nickName",etContent);
                    isSave=true;
                }
                else {
                    Toast.makeText(changeUserInfoActivity_yl.this,"昵称不能为空",Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                if(!TextUtils.isEmpty(etContent)) {
                    data.putExtra("signature",etContent);
                    isSave=true;
                }
                else {
                    Toast.makeText(changeUserInfoActivity_yl.this,"签名不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        if(isSave){
            setResult(RESULT_OK,data);
            Toast.makeText(changeUserInfoActivity_yl.this,"保存成功",Toast.LENGTH_SHORT).show();
            changeUserInfoActivity_yl.this.finish();
        }
    }

    /**
     *字符截取功能
     */
    private void cutOut(Editable editable, int len, int maxLen){
        if (len>maxLen){
            int selEndIndex= Selection.getSelectionEnd(editable);
            String str=editable.toString();
            //截取新字符串
            String newStr=str.substring(0,maxLen);
            et_content.setText(newStr);
            editable=et_content.getText();
            //新字符串长度
            int newLen=editable.length();
            if(selEndIndex>newLen){
                selEndIndex=editable.length();
            }
            Selection.setSelection(editable,selEndIndex);
        }
    }

    /**
     * 文本框内容修改监听功能
     */
    private void contentListener(){
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable=et_content.getText();
                int len=editable.length();
                if(len>0){
                    iv_delete.setVisibility(View.VISIBLE);
                }
                else {
                    iv_delete.setVisibility(View.GONE);
                }
                switch (flag_yl) {
                    case 1:
                        cutOut(editable,len,8);
                        break;
                    case 2:
                        cutOut(editable,len,16);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
